package com.example.demorsa.serivce;

/**
 * Copyright (c) 2018-2022 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 */

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.KeyPair;

import javax.annotation.PostConstruct;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liyuncong
 * @version 1.0
 * @file
 * @brief
 * @details
 * @date 2022-09-27
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022-09-27                      liyuncong          Created
 */
//@Service
public class RsaService2 {

    @PostConstruct
    public static Map<String, String> getKeyMap() {
        Map<String, String> keys = new HashMap<>();
        int type = KeyPair.RSA;
        JSch jsch = new JSch();

        File pub = new File("E:\\temp\\a");
        File pri = new File("E:\\temp\\b");


//        JSch.setConfig();
        try {
            KeyPair kpair = KeyPair.genKeyPair(jsch, type, 2048);
            //私钥
            ByteArrayOutputStream baos = new ByteArrayOutputStream();//向OutPutStream中写入
            kpair.writePrivateKey(baos);
            String privateKeyString = baos.toString();
            //公钥
            baos = new ByteArrayOutputStream();
            kpair.writePublicKey(baos, "zhangsan@qq.com");
            String publicKeyString = baos.toString();
            System.out.println("Finger print: " + kpair.getFingerPrint());
            kpair.dispose();
            // 得到公钥字符串
//			String publicKeyString = RSAEncrypt.loadPublicKeyByFile(filePath,filename + ".pub");
//			System.out.println(publicKeyString.length());
            System.out.println(publicKeyString);
            keys.put("publicKey", publicKeyString);
            // 得到私钥字符串
//			String privateKeyString = RSAEncrypt.loadPrivateKeyByFile(filePath,filename);
//			System.out.println(privateKeyString.length());
            System.out.println(privateKeyString);
            keys.put("privateKey", privateKeyString);
        } catch (Exception e) {
            System.out.println(e);
        }
        return keys;
    }

}
